class Set {
    constructor () {
      this.items = {}
    }
    has (element) {
      return element in this.items
    }
    add (element) {
      if (!this.has(element)) {
        this.items[element] = element
        return true
      }
      return false
    }
    delete (element) {
      if (this.has(element)) {
        delete this.items[element]
        return true
      }
      return false
    }
    clear () {
      this.items = {}
    }
    size () {
      return Object.keys(this.items).length
    }
    values () {
      return Object.values(this.items)
    }
}


function findLucky(arr) {
  const freqMap = new Map();
  let maxLucky = -1;

  // 构建频率映射
  for (const num of arr) {
      freqMap.set(num, (freqMap.get(num) || 0) + 1);
  }

  // 检查幸运数
  for (const [num, freq] of freqMap) {
      if (num === freq && num > maxLucky) {
          maxLucky = num;
      }
  }

  return maxLucky;
}

// 示例
console.log(findLucky([2, 2, 3, 4])); // 输出: 2
console.log(findLucky([1, 2, 2, 3, 3, 3])); // 输出: 3